package com.code8086.main;

import java.io.File;
import java.util.Scanner;

import com.code8086.working.DateTimeConvertor;
import com.code8086.working.Employee;

import jxl.Sheet;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class Main
{
	public static void main (String[] args) throws Exception
	{
		String xls_file_path = "all.xls";
		Workbook book = Workbook.getWorkbook(new File(xls_file_path));
		int sheet_count = book.getNumberOfSheets();
		System.out.println(sheet_count);
		Employee[] employee = new Employee[sheet_count];
		for (int i = 0; i < sheet_count; i++)
		{
			employee[i] = new Employee();
		}
		for (int i = 0; i < sheet_count; i++)
		{
			Sheet sheet = book.getSheet(i);
			employee[i].Name = sheet.getName();
			employee[i].Type = sheet.getCell(12,1).getContents();
			int index = -1;
			for (int j = 9; j <= 24; j++)
			{
				index++;
				for (int point_index = 0; point_index < 6; point_index++)
				{
					employee[i].work[index].point[point_index] = DateTimeConvertor.timeToDouble( sheet.getCell(point_index+1, j).getContents() );
				}
			}
			for (int j = 9; j <= 23; j++)
			{
				index++;
				for (int point_index = 0; point_index < 6; point_index++)
				{
					employee[i].work[index].point[point_index] = DateTimeConvertor.timeToDouble( sheet.getCell(point_index+10, j).getContents() );
				}
			}
		}
		
		// ---- 计算每个人的总工作时间和标准时间 ----
		for (int i = 0; i < sheet_count; i++)
		{
			employee[i].calTotalWorkingTime();
			employee[i].calStandartWorkingTime();
		}
		
		// ---- 根据special.ini文件进行调整每个人的总工作时间和标准时间
		Scanner scanner = new Scanner(new File("special.ini"));
		while (scanner.hasNext())
		{
			String str = scanner.nextLine();
			String[] cmd = str.split("%");
			if (cmd.length == 3)
			{
				// ---- 格式正确，开始动手
				if (cmd[0].equals("ALL"))
				{
					// ---- 面对所有人进行的修改
					for (int i = 0; i < sheet_count; i++)
					{
						if (cmd[1].equals("总"))
						{
							employee[i].total_working_time += Double.valueOf(cmd[2]);
						}
						if (cmd[1].equals("标准"))
						{
							employee[i].standard_working_time += Double.valueOf(cmd[2]);
						}
					}
					
				}
				else
				{
					
					// ---- 面对单个人进行的修改
					for (int i = 0; i < sheet_count; i++)
					{
						if (employee[i].Name.split("_")[0].equals(cmd[0]))
						{
							if (cmd[1].equals("总"))
							{
								employee[i].total_working_time += Double.valueOf(cmd[2]);
							}
							if (cmd[1].equals("标准"))
							{
								employee[i].standard_working_time += Double.valueOf(cmd[2]);
							}
						}
					}
				}
			}
		}
		scanner.close();
		
		// ---- 计算每个人的加班时间 ----
		for (int i = 0; i < sheet_count; i++)
		{
			employee[i].calExtraWorkingTime();
		}
		
		// ---- 统一输出每个员工的信息
		for (int i = 0; i < sheet_count; i++)
		{
			//employee[i].outputInfo();
			employee[i].outputInfoFormat();
		}
		
		// ---- 生成员工的工作时间情况Excel文件
		WritableWorkbook writebook = Workbook.createWorkbook(new File("报表.xls"));
		WritableSheet sheet=writebook.createSheet("报表",0); 
		sheet.addCell(new Label(2,1,"姓名"));;
		sheet.addCell(new Label(3,1,"编号"));;
		sheet.addCell(new Label(4,1,"类型"));;
		sheet.addCell(new Label(5,1,"总工作时间"));;
		sheet.addCell(new Label(6,1,"标准时间"));;
		sheet.addCell(new Label(7,1,"加班时间"));;
		
		for (int i = 0; i < sheet_count; i++)
		{
			Label label_name = new Label(2,i+2,employee[i].Name.split("_")[0]);
			Label label_number = new Label(3,i+2,employee[i].Name.split("_")[1]);
			Label label_type = new Label(4,i+2,employee[i].Type);
			Label label_total = new Label(5,i+2, String.format("%.2f", employee[i].total_working_time).toString() );
			Label label_standard = new Label(6,i+2, String.format("%.2f", employee[i].standard_working_time).toString() );
			Label label_extra = new Label(7,i+2, String.format("%.2f", employee[i].extra_working_time).toString() );
			sheet.addCell(label_name);
			sheet.addCell(label_number);
			sheet.addCell(label_type);
			sheet.addCell(label_total);
			sheet.addCell(label_standard);
			sheet.addCell(label_extra);
		}
		writebook.write();
		writebook.close();
	}
}
